/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       direct_surface_16.cpp

	$Header: /game/direct_surface_16.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "direct_surface_16.h"

#include "pixel_24.h"
#include "pixel_format.h"
#include "surface_description.h"

// -------------------------------------------------------
// 16 bit direct draw surface
// -------------------------------------------------------
t_direct_surface_16::t_direct_surface_16( int width, int height, IDirectDraw7* direct_draw,
										  int red_mask, int green_mask, int blue_mask )
{
	t_surface_description description;
	t_pixel_format        pixel_format;

	m_width = width;
	m_height = height;
	set_16_bit( pixel_format, red_mask, green_mask, blue_mask );
	set_size( description, width, height );
	set_pixel_format( description, pixel_format );
	
	m_surface = 0;
	if (direct_draw->CreateSurface( &description, &m_surface, 0 ) != 0)
		m_surface = 0;
}

// -------------------------------------------------------
// 16 bit direct draw surface
// -------------------------------------------------------
t_direct_surface_16::~t_direct_surface_16()
{
	if (m_surface != 0)
	{
		m_surface->Release();
	}
}

// -------------------------------------------------------
// 16 bit direct draw surface adaptor
// -------------------------------------------------------
t_surface_adaptor_16::t_surface_adaptor_16( IDirectDrawSurface7* surface, 
										    t_screen_point size )
                    : m_surface( surface )
{
	t_surface_description description;

	if (surface->Lock( 0, &description, DDLOCK_NOSYSLOCK | DDLOCK_WAIT, 0 ) == 0)
	{
		if (size.x > (int)description.dwWidth)
			size.x = (int)description.dwWidth;
		if (size.y > (int)description.dwHeight)
			size.y = (int)description.dwHeight;
		init( size.x, size.y, description.lPitch, (t_uint16*)(description.lpSurface) );
	}
}

// -------------------------------------------------------
// 16 bit direct draw surface adaptor
// -------------------------------------------------------
t_surface_adaptor_16::~t_surface_adaptor_16()
{
	if (get_data_ptr() != 0)
		m_surface->Unlock( 0 );
}

IDirectDraw7* get_direct_draw( IDirectDrawSurface7* surface )
{
	IDirectDraw7* result;

	if (surface->GetDDInterface( reinterpret_cast<void**>( &result ) ) != 0)
		return 0;
	return result;
}
